﻿using System;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using System.Collections.Generic;
using System.Configuration;

using SBPweb.Forge.Communication.WCF;

namespace SBPweb.GoogleDataAPI.Server.ServiceHost
{

    public class CustomServiceHost : CustomHost
    {

        #region Private fields

        private static HttpClientCredentialType authType;

        #endregion

        #region Constructor logic

        static CustomServiceHost()
        {
            string auth = ConfigurationSettings.AppSettings["wcfHostAuthentication"];
            if (string.IsNullOrEmpty(auth))
            {
                authType = HttpClientCredentialType.Windows;
            }
            else
            {
                authType = (HttpClientCredentialType)Enum.Parse(typeof(HttpClientCredentialType), auth);
            }
        }

        public CustomServiceHost(Type serviceType, Type contractType, params Uri[] baseAddresses)
            : base(serviceType, contractType, GetDnsSafeUri(baseAddresses))
        {
            this.IsCustomFaultBehaviorActive = false;
            this.IsServiceDebugBehaviorActive = true;
        }

        #endregion

        #region Methods

        protected override Binding CreateBindingInstanceForHttp(Type type, Uri address)
        {
            BasicHttpBinding ret = new BasicHttpBinding(BasicHttpSecurityMode.TransportCredentialOnly);
            ret.Security.Transport.ClientCredentialType = authType;
            return ret;
        }

        protected override void CreateBindings()
        {
            Binding httpBinding = CreateBindingInstanceForHttp(ContractType, dictUrisByScheme["http"]);
            if (httpBinding != null)
            {
                ServiceEndpoint sep = this.AddServiceEndpoint(
                    ContractType,
                    httpBinding,
                    dictUrisByScheme["http"]);
            }
            dictBindings.Add("http", httpBinding);
            dictUrisByScheme.Remove("net.tcp");
            //Binding tcpBinding = CreateBindingInstanceForTcp(ContractType, dictUrisByScheme["net.tcp"]);
            //if (tcpBinding != null)
            //{
            //    this.AddServiceEndpoint(
            //        ContractType,
            //        tcpBinding,
            //        dictUrisByScheme["net.tcp"]);
            //}

            //Binding mexBinding = MetadataExchangeBindings.CreateMexTcpBinding();
            //if (dictUrisByScheme.ContainsKey("net.tcp"))
            //{
            //    this.AddServiceEndpoint(
            //        typeof(IMetadataExchange),
            //        mexBinding,
            //        string.Format("{0}/mex", dictUrisByScheme["net.tcp"]));
            //}
        }

        //protected override void CreateExtensions()
        //{
        //    CustomFaultBehavior cfb = new CustomFaultBehavior();
        //    this.Extensions.Add(cfb);
        //}

        protected override void OnFaulted()
        {
            base.OnFaulted();
        }

        private static Uri[] GetDnsSafeUri(Uri[] addresses)
        {
            List<Uri> ret = new List<Uri>();
            foreach (Uri u in addresses)
            {
                if (u.DnsSafeHost == ConfigurationSettings.AppSettings["dnsSafeHost"])
                {
                    ret.Add(u);
                }
            }
            if (ret.Count == 0 && addresses.Length > 0)
            {
                ret.Add(addresses[0]);
            }
            return ret.ToArray();
        }
        
        #endregion

    }

}